//
//  SegmentImageButton.swift
//  xgwjinterconnection
//  自定义分段图片按钮
//  Created by WangWeiShuo on 2025/8/19.
//

import SwiftUI
struct SegmentImageItem : Hashable {
      var iconNormal : String = ""
      var iconPressed : String = ""
      var selectionIndex = 0
}

struct SegmentImageButton : View {

      @Binding var selection : Int
      var items : [SegmentImageItem]

      var body : some View {

            let width : CGFloat = 110.0

            return HStack(spacing: 5) {
                  ForEach (items, id: \.self) { item in
                      SegmentImageButtonView(iconNormal: item.iconNormal, iconPressed: item.iconPressed,width: width, selectionIndex: item.selectionIndex, selection: self.$selection)
                          .modifier(ProportionLayout(proportion: 1))
                  }
            }.font(.body)
                  .padding()
                  .background(Color("color_gray_f3f3f3"))
                  .cornerRadius(10.0)
      }
}

struct SegmentImageButtonView : View {
    
    var iconNormal : String = ""
    var iconPressed : String = ""
    var width : CGFloat
    var selectionIndex = 0
    @Binding var selection : Int
    
    var body : some View {
        let label = Image(selection == selectionIndex ? iconPressed: iconNormal)
            .resizable()
            .scaledToFit()
            .cornerRadius(10.0)
            .foregroundColor(Color.white)
        
        return Button(action: { self.selection = self.selectionIndex }) { label }
    }
}


struct ProportionLayout: ViewModifier {
    let proportion: CGFloat
    
    func body(content: Content) -> some View {
        content
            .frame(minWidth: 0, maxWidth: .infinity)
            .layoutPriority(proportion)
    }
}
